package com.cee.admin.modules.cc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.core.query.BasePageQuery;
import com.cee.common.exception.GlobalException;
import com.cee.common.utils.StringUtils;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.cc.entity.CcEmissionFactor;
import com.cee.domain.modules.cc.form.add.CcEmissionFactorAddForm;
import com.cee.domain.modules.cc.form.update.CcEmissionFactorUpdateForm;
import com.cee.domain.modules.cc.vo.CcEmissionFactorVo;
import com.cee.ds.common.constants.PermissionConstants;
import com.cee.ds.modules.cc.service.ICcEmissionFactorService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cee.admin.common.BaseController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 排放因子 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2023-12-22
 */
@RestController
@RequestMapping("/api/v1/carbonComponent/carbonEmissionFactor")
@Tag(name = "碳组件—排放因子")
public class CcEmissionFactorController extends BaseController {

    @Autowired
    private ICcEmissionFactorService emissionFactorService;

    @GetMapping("/page")
    @Operation(summary = "分页")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_FACTOR_PREFIX + PermissionConstants.READ)
    public Result<Page<CcEmissionFactorVo>> page(BasePageQuery<CcEmissionFactorVo> query, String sourceName) {
        CcEmissionFactorVo vo = new CcEmissionFactorVo();
        vo.setSourceName(sourceName);
        return success(emissionFactorService.queryPageVo(query.generatePage(), vo));
    }

    @GetMapping("/get")
    @Operation(summary = "获取排放因子", description = "优先级=name>id>sourceId，sourceId={54=电力，55=热力，56=石灰石}")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_FACTOR_PREFIX + PermissionConstants.READ)
    public Result<CcEmissionFactor> get(@Parameter(description = "排放因子ID") Integer id,
                                        @Parameter(description = "排放源ID") Integer sourceId,
                                        @Parameter(description = "排放源名称") String name) {
        CcEmissionFactor queryEntity = null;
        if (name != null) {
            queryEntity = emissionFactorService.getByName(name);
            if (queryEntity != null)
                return success(queryEntity);
        }
        if (id != null || sourceId != null) {
            queryEntity = emissionFactorService.lambdaQuery()
                    .eq(id!=null,CcEmissionFactor::getId, id)
                    .eq(sourceId!= null,CcEmissionFactor::getSourceId, sourceId)
                    .one();
        }
        if (queryEntity != null) {
            return success(queryEntity);
        }
        throw new GlobalException("数据未录入");
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_FACTOR_PREFIX + PermissionConstants.ADD)
    public Result<Boolean> add(@RequestBody @Valid CcEmissionFactorAddForm addForm) {
        return success(emissionFactorService.save(addForm.toEntity()));
    }

    @PostMapping("/update")
    @Operation(summary = "更新")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_FACTOR_PREFIX + PermissionConstants.UPDATE)
    public Result<Boolean> update(@RequestBody @Valid CcEmissionFactorUpdateForm updateForm) {
        return success(emissionFactorService.updateById(updateForm.toEntity()));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_FACTOR_PREFIX + PermissionConstants.DEL)
    public Result<Boolean> delete(@PathVariable("id") Integer id)  {
        return success(emissionFactorService.removeById(id));
    }

    @Operation(summary = "查询所有因子列表")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_FACTOR_PREFIX + PermissionConstants.READ)
    public Result<List<CcEmissionFactor>> findAll(){
        return success(emissionFactorService.list());
    }

}

